<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="百度地图,百度地图API，百度地图自定义工具，百度地图所见即所得工具" />
<meta name="description" content="百度地图API自定义地图，帮助用户在可视化操作下生成百度地图" />
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css" />
<style type="text/css">
.pull-right button {
	height: 34px;
}
</style>
<!--引用百度地图API-->
</head>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
	<div class="row">
		<div class="col-sm-12">
			<table id="table">
				<thead style="background-color: #00C0EF">
					<!-- 					<tr style="background-color: #00C0EF"> -->
					<th class="text-center" data-field="name">群团名称</th>
					<th class="text-center" data-field="addr">地址</th>
					<th class="text-center" data-field="point">坐标</th>
					<th class="text-center" data-field="createTime">创建时间</th>
					<th class="text-center" data-field="action" data-formatter="actionFormatter" data-events="actionEvents">操作</th>
					</tr>
				</thead>
			</table>
		</div>
	</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document" style="width: 698px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">找群团详情</h4>
				</div>
				<div class="modal-body">
					<div class="input-group">
						<span class="input-group-addon">群团名称：</span> <input type="text" class="form-control" id="name1">
					</div>
					<br />
					<div class="input-group">
						<span class="input-group-addon">地址：</span> <input type="text" class="form-control" id="addr"> <span class="input-group-btn">
								<button class="btn btn-default" type="button" onclick="search();">搜索</button>
						</span>
					</div>
					<br />
					<div class="input-group">
						<span class="input-group-addon">坐标：</span> <input type="text" class="form-control" id="axis" readonly="readonly">
					</div>
					<br />
					<div style="width: 100%; height: 500px; border: #ccc solid 1px;" id="dituContent"></div>
				</div>
				<div class="modal-footer">
					<button id="submitBtn" type="button" class="btn btn-primary" onclick="submitMien();">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=Pe44jCEWdEGa4LN8t6Mta0AQGUu1dZer"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script type="text/javascript">
	var selectId = "";
	//创建和初始化地图函数：
	function initMap() {
		createMap();//创建地图
		setMapEvent();//设置地图事件
		addMapControl();//向地图添加控件
	}

	//创建地图函数：
	function createMap() {
		var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
		// 		var point = new BMap.Point(114.311831, 30.598428);//定义一个中心点坐标
		map.centerAndZoom("孝感", 15);//设定地图的中心点和坐标并将地图显示在地图容器中
		map.addEventListener("click", function(e) {
			$("#axis").val(e.point.lng + "," + e.point.lat);
		});
		window.map = map;//将map变量存储在全局
	}

	//地图事件设置函数：
	function setMapEvent() {
		map.enableDragging();//启用地图拖拽事件，默认启用(可不写)
		map.enableScrollWheelZoom();//启用地图滚轮放大缩小
		map.enableDoubleClickZoom();//启用鼠标双击放大，默认启用(可不写)
		map.enableKeyboard();//启用键盘上下左右键移动地图
	}

	//地图控件添加函数：
	function addMapControl() {
		//向地图中添加缩放控件
		var ctrl_nav = new BMap.NavigationControl({
			anchor : BMAP_ANCHOR_TOP_LEFT,
			type : BMAP_NAVIGATION_CONTROL_LARGE
		});
		map.addControl(ctrl_nav);
		//向地图中添加缩略图控件
		var ctrl_ove = new BMap.OverviewMapControl({
			anchor : BMAP_ANCHOR_BOTTOM_RIGHT,
			isOpen : 1
		});
		map.addControl(ctrl_ove);
		//向地图中添加比例尺控件
		var ctrl_sca = new BMap.ScaleControl({
			anchor : BMAP_ANCHOR_BOTTOM_LEFT
		});
		map.addControl(ctrl_sca);
	}

	initMap();//创建和初始化地图

	function actionFormatter(value, row, index) {
		return [ "<button class='btn btn-primary btn-xs like' onclick='showModal(\"" + row.id + "\");'>详情信息</button>",
				"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='del(\"" + row.id + "\")'>删除信息</button>", ].join('');
	}

	var table_dataset = [ {
		field : 'name',
		align : 'center',
	}, {
		field : 'addr',
		align : 'center',
	}, {
		field : 'point',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return row.axisx + "," + row.axisy;
		}
	}, {
		field : 'createTime',
		align : 'center',
		formatter : function timeFormatter(value, row, index) {
			return (new Date(row.createTime)).Format("yyyy-MM-dd hh:mm:ss");
		}
	}, {
		align : 'center',
		formatter : actionFormatter,
	} ];

	$('#table').bootstrapTable(
			{
				url : "${pageContext.request.contextPath}/selectGroups",
				idField : "id",
				columns : table_dataset,
				striped : true, //是否显示行间隔色
				pagination : true, //是否显示分页
				pageSize : 10, //每页的记录行数
				pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
				// 		search : true, //是否显示搜索
				showRefresh : true,
				sidePagination : "server", //表示服务端请求  
				queryParamsType : "limit",
				queryParams : function queryParams(params) { //设置查询参数  
					var param = {
						offset : params.offset,
						limit : params.limit,
						name : $("#name").val()
					};
					return param;
				},
				toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>"
						+ "<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'>"
						+ "<input type='text' class='form-control' placeholder='请输入名称进行查询...' id='name' name='name'>"
						+ "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='Querry();'>查询</button></span></div>",
				icons : {
					paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
					paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
					refresh : 'glyphicon-refresh icon-refresh',
					toggle : 'glyphicon-list-alt icon-list-alt',
					columns : 'glyphicon-th icon-th',
					detailOpen : 'glyphicon-plus icon-plus',
					detailClose : 'glyphicon-minus icon-minus',
					"export" : 'glyphicon-export icon-share'
				},
				showExport : true,
				exportDataType : 'basic',
				exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ]
			});

	function showModal(id) {
		selectId = id;
		map.clearOverlays();
		if (id == '') {
			$("#name1").val('');
			$("#addr").val('');
			$("#axis").val('');
			map.reset();
		} else {
			$.ajax({
				url : "${pageContext.request.contextPath}/selectGroupById",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					var group = data.group;
					$("#name1").val(group.name);
					$("#addr").val(group.addr);
					$("#axis").val(group.axisx + "," + group.axisy);
					var point = new BMap.Point(group.axisx, group.axisy);//定义一个中心点坐标
					var mk = new BMap.Marker(point);
					map.addOverlay(mk);
					map.panTo(point);
				}
			});
		}
		$('#modal').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function submitMien() {
		$("#submitBtn").button('loading');
		var name = $("#name1").val();
		var addr = $("#addr").val();
		var axis = $("#axis").val();
		if (name == '' || addr == '' || axis == '') {
			layer.alert("数据填写不完整!");
			$("#submitBtn").button('reset');
			return;
		}
		var axisx = axis.split(",")[0];
		var axisy = axis.split(",")[1];
		$.ajax({
			url : "${pageContext.request.contextPath}/insertAndUpdateGroup",
			type : "post",
			data : {
				id : selectId,
				name : name,
				addr : addr,
				axisx : axisx,
				axisy : axisy
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#modal").modal("hide");
						$("#table").bootstrapTable('refresh');
					}, 1000);
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
				$("#submitBtn").button('reset');
			}
		});
	}

	function del(id) {
		layer.confirm('确认删除吗？', function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/insertAndUpdateGroup",
				type : "post",
				data : {
					id : id,
					s1 : '-1'
				},
				dataType : "json",
				success : function(data) {
					var result = data['result'];
					if (result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 1000);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				}
			});
		});
	}

	function search() {
		var addr = $("#addr").val();
		if (addr == '') {
			return;
		}
		map.clearOverlays();
		var local = new BMap.LocalSearch(map, {
			renderOptions : {
				map : map
			}
		});
		local.search(addr);
	}

	function Querry() {
		$("#table").bootstrapTable('refresh');
	}
</script>
</html>